package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.AdSpace;
import cn.nubia.admin.application.dto.AdSpaceDTO;
import cn.nubia.admin.application.query.AdSpaceQuery;
import cn.nubia.admin.application.bean.Terminal;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by cnJason on 2015/7/6.
 */
@Service
public class AdSpaceService extends BaseServiceImpl<AdSpace> {

    @Autowired
    private TerminalService terminalService;

    @Autowired
    private AdItemService adItemService;

    @Autowired
    SystemLogService systemLogService;

    /**
     * 查询广告位列表.
     *
     * @param adSpaceQuery 广告位查询条件
     * @return 广告列表分页.
     */
    public Paged<AdSpaceDTO> findPage(AdSpaceQuery adSpaceQuery) {

        Query<AdSpace> query = Query.build(AdSpace.class);

        //设置id
        query.addEq("id", adSpaceQuery.getAdSpaceId());
        //设置名称
        if (adSpaceQuery.getAdSpaceName() != "") {
            query.addLike("adSpaceName", adSpaceQuery.getAdSpaceName());
        }
        String range = adSpaceQuery.getCreateDateRange();
        Date createDateBegin = null;
        Date createDateEnd = null;
        if (range != null&&!"".equals(range)) {
            createDateBegin = DateUtil.getBeginDateFromRange(range);
            createDateEnd = DateUtil.getEndDateFromRage(range);
        }
        if(createDateBegin!=null&&createDateEnd!=null){
            //设置创建时间
            query.addBetween("createTime", DateUtil.getIntegerTime(createDateBegin), DateUtil.getIntegerTime(createDateEnd));
        }

        //设置状态
        if(adSpaceQuery.getState()!=null){
            query.addEq("state", adSpaceQuery.getState());
        }else{
            query.addNotEq(AdSpace.COLUMN_STATE,AdSpace.STATE_DEL);
        }


        //设置页码
        query.setPaged(adSpaceQuery.getPageNo(), adSpaceQuery.getPageSize());
        query.addOrder("createTime", Query.DBOrder.DESC);
        Paged<AdSpace> paged = findPagedByQuery(query);
        Paged<AdSpaceDTO> adSpaceDTOPaged = new Paged<AdSpaceDTO>();

        //转换对象.
        adSpaceDTOPaged.setPageNo(paged.getPageNo());
        adSpaceDTOPaged.setPageSize(paged.getPageSize());
        adSpaceDTOPaged.setTotalCount(paged.getTotalCount());

        //转换
        List<AdSpace> list = paged.getListData();
        List<AdSpaceDTO> dtoList = new ArrayList<AdSpaceDTO>();

        for (AdSpace adSpace : list) {
            AdSpaceDTO adSpaceDTO = new AdSpaceDTO();
            BeanUtils.copyProperties(adSpace, adSpaceDTO);

            Terminal terminal = terminalService.get(adSpaceDTO.getTerminalId());
            //设置终端名称.
            if(terminal != null){
                adSpaceDTO.setTerminalName(terminal.getTerminalName());
            }

            //设置该广告位包含的广告数.
            adSpaceDTO.setAdItemCount(adItemService.countBySpaceId(adSpaceDTO.getId()));

            dtoList.add(adSpaceDTO);
        }
        adSpaceDTOPaged.setListData(dtoList);

        return adSpaceDTOPaged;
    }

    /**
     * 保存广告位信息.
     *
     * @param adSpace 广告位对象
     */
    public void save(AdSpace adSpace) {
        Integer currTime = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        adSpace.setLastModifyTime(currTime);
        if (null == adSpace.getId()) {
            adSpace.setCreateTime(currTime);
            adSpace.setCreateAdminName(adminName);
            adSpace.setCreateAdminId(adminId);
            insert(adSpace);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.ADSPACELIST, SystemLog.OperateTypeEnum.ADD, adSpace.getId());
        } else {
            AdSpace domain = get(adSpace.getId());
            adSpace.copyStakeProperties(domain);
            adSpace.setModifyAdminId(adminId);
            adSpace.setModifyAdminName(adminName);
            update(adSpace);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.ADSPACELIST, SystemLog.OperateTypeEnum.EDIT, adSpace.getId());
        }
    }

    public List<AdSpace> getAdSpaceByName(String adSpaceName) {
        Query<AdSpace> query = Query.build(AdSpace.class);
        query.addLike("adSpaceName", adSpaceName);
        return findByQuery(query);
    }

    public void deleteAdSpace(Integer adSpaceId) {
        AdSpace adSpace = get(adSpaceId);
        if (adSpace != null) {
            adSpace.setState(AdSpace.DELETE_STATE);
            Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
            String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
            Integer curr = DateUtil.getCurrentTime();
            adSpace.setModifyAdminName(adminName);
            adSpace.setModifyAdminId(adminId);
            adSpace.setLastModifyTime(curr);
            update(adSpace);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.ADSPACELIST, SystemLog.OperateTypeEnum.ADD, adSpaceId);
        }
    }
}
